import { Metadata } from 'next';
import { redirect } from 'next/navigation';
import { cache } from 'react';
interface OrderPageProps {
  params: { id: string };
}

const fetchOrderData = cache(async (id: string) => {
  const res = await fetch(`http://localhost:3000/api/orders/${id}`, {
    cache: 'force-cache', // SSR 动态获取
    next: {
      revalidate: 60, // 缓存 60 秒
    },
  });

  const orderData = res.ok ? await res.json() : null;

  if (!orderData) {
    return {};
  }

  return orderData;
});
export async function generateMetadata({
  params,
}: OrderPageProps): Promise<Metadata> {
  const { id } = await params; // 显式
  const orderData = await fetchOrderData(id);

  if (!orderData) {
    return {};
  }
  return {
    title: `${orderData.title} - 我的应用`,
    description: orderData.id,
    openGraph: {
      title: `订单 ${id}`,
      description: `订单 ${id} - 状态：1`,
      url: `https://your-site.com/order/${id}`,
      siteName: '我的应用',
      locale: 'zh_CN',
      type: 'article',
    },
  };
}
export default async function OrderDetail({ params }: OrderPageProps) {
  const { id } = await params; // 显式 await params
  // 调用 API
  const orderData = await fetchOrderData(id);

  if (!orderData) {
    redirect('/order/not-found');
  }
  return (
    <>
      <div>OrderDetail</div>
      <p>订单 ID: {orderData.id}</p>
      <p>标题: {orderData.title}</p>
      <p>状态: {orderData.status}</p>
    </>
  );
}
